package com.ithema.easy.controller;

/**
 * @ClassName SmsController.java
 * @Description TODO
 */

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sms")
@Log
public class SmsController {

    @Autowired
    Client queryClient;
    @GetMapping("send/{mobile}")
    public boolean send(@PathVariable("mobile") String mobile){
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        //接收短信的手机号码，JSON数组格式。
        sendSmsRequest.setPhoneNumbers(mobile);
        //请在控制台国内消息或国际/港澳台消息页面中的签名管理页签下签名名称一列查看
        sendSmsRequest.setSignName("传智2024");
        //请在控制台国内消息或国际/港澳台消息页面中的签名管理页签下签名名称一列查看。
        sendSmsRequest.setTemplateCode("SMS_464500302");
        //构建模板参数替换
        Map<String, String> templateParams = new HashMap<>();
        templateParams.put("code",String.valueOf((int)((Math.random()*9+1)*100000)));
        //短信模板变量对应的实际值，JSON格式。
        sendSmsRequest.setTemplateParam(JSONObject.toJSONString(templateParams));
        try {
            SendSmsResponse sendSmsResponse = queryClient.sendSms(sendSmsRequest);
            System.out.println(JSONObject.toJSONString(sendSmsResponse));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
